<template>
  <div class="user-avatar-dropdown">
    <Dropdown @on-click="handleClick">
      <Badge :dot="!!messageUnreadCount">
        <Avatar :src="userAvatar"/>
      </Badge>
      <span style="margin: 0px 6px;font-size: 14px;vertical-align: middle;">{{userName}}</span>
      <span style="vertical-align: middle;"><i>Lv.{{userLevel}}</i></span>
      <Icon :size="18" type="md-arrow-dropdown"></Icon>
      <DropdownMenu slot="list">
        <!-- <DropdownItem name="message">
          消息中心<Badge style="margin-left: 10px" :count="messageUnreadCount"></Badge>
        </DropdownItem> -->
        <DropdownItem name="logout">
          <Icon type="md-log-out" :size="16"></Icon>
          <span>退出登录</span>
        </DropdownItem>
      </DropdownMenu>
    </Dropdown>

    <Modal v-model="logoutModal" width="360" class="logout-modal">
      <p slot="header" style="color: #f60;">
        <Icon type="ios-information-circle"></Icon>
        <span>系统信息</span>
      </p>
      <div style="text-align:center; font-size: 16px;">
        <p>确定退出系统？</p>
      </div>
      <div slot="footer">
        <Button type="error" size="large" long :loading="logoutLoading" @click="logout">退出</Button>
      </div>
    </Modal>
  </div>
</template>

<script>
import './user.styl'
import { mapActions } from 'vuex'
export default {
  name: 'User',
  props: {
    userAvatar: {
      type: String,
      default: ''
    },
    userName: {
      type: String,
      default: '-'
    },
    userLevel: {
      type: String | Number,
      default: '-'
    },
    messageUnreadCount: {
      type: Number,
      default: 0
    }
  },
  data () {
    return {
      logoutModal: false,
      logoutLoading: false
    }
  },
  methods: {
    ...mapActions([
      'handleLogOut',
      'setTagNavList'
    ]),
    // 咨询是否退出系统
    whetherLogout () {
      this.logoutModal = true
    },
    // 退出系统
    logout () {
      // this.handleLogOut().then(() => {
      //   this.$router.push({
      //     name: 'login'
      //   })
      // })
      this.handleLogOut().then(() => {
        this.setTagNavList([])
        this.$store.commit('setCurrentTabs', '')
        window.sessionStorage.removeItem('currentTabs')
        window.sessionStorage.removeItem('tagNaveList')
        // window.sessionStorage.removeItem('tokenJAE')
        // window.sessionStorage.removeItem('accessToken')
        window.sessionStorage.removeItem('userLogin')
        window.sessionStorage.removeItem('username')
        window.sessionStorage.removeItem('userInfo')
        // this.$router.push({
        //   path: '/login'
        // })
        // window.location.replace('http://10.1.80.84:8504/app/ai-login/')
        window.sessionStorage.removeItem('tokenJAE')
        window.sessionStorage.removeItem('msa_token')
        window.sessionStorage.removeItem('oauth_token')
        window.sessionStorage.removeItem('loginUserInfo')
        window.localStorage.removeItem('loginUserInfo')
        window.sessionStorage.removeItem('prevProTemp')
        window.sessionStorage.removeItem('prevDevTemp')
        // window.location.replace('/app/iot-login/')
        this.$router.push({
          path: '/login'
        })
      })
    },
    message () {
      this.$router.push({
        name: 'message_page'
      })
    },
    handleClick (name) {
      switch (name) {
        case 'logout': this.whetherLogout()
          break
        case 'message': this.message()
          break
      }
    }
  }
}
</script>

<style lang="stylus" scoped>
.logout-modal
  & >>> .ivu-modal
    top 200px
.user-avatar-dropdown
  & >>> .ivu-dropdown-item
    .ivu-icon
      vertical-align middle
      margin-right 2px
    span
      vertical-align middle
</style>
